package com.example.musicaiap.data.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.musicaiap.data.database.CurrentPlaylistEntity
import kotlinx.coroutines.flow.Flow

/**
 * 当前播放列表数据访问对象
 * 管理当前播放列表的状态
 */
@Dao
interface CurrentPlaylistDao {
    
    /**
     * 插入当前播放列表信息
     * 因为ID固定为1，如果已存在则替换
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertCurrentPlaylist(currentPlaylist: CurrentPlaylistEntity)
    
    /**
     * 更新当前播放列表信息
     */
    @Update
    suspend fun updateCurrentPlaylist(currentPlaylist: CurrentPlaylistEntity)
    
    /**
     * 获取当前播放列表信息
     */
    @Query("SELECT * FROM current_playlist WHERE id = 1")
    suspend fun getCurrentPlaylist(): CurrentPlaylistEntity?
    
    /**
     * 获取当前播放列表信息（Flow版本）
     */
    @Query("SELECT * FROM current_playlist WHERE id = 1")
    fun getCurrentPlaylistFlow(): Flow<CurrentPlaylistEntity?>
    
    /**
     * 更新当前播放歌曲ID
     */
    @Query("UPDATE current_playlist SET currentSongId = :songId, lastPlayedAt = :timestamp WHERE id = 1")
    suspend fun updateCurrentSongId(songId: String, timestamp: Long = System.currentTimeMillis())
    
    /**
     * 更新当前播放位置
     */
    @Query("UPDATE current_playlist SET currentPosition = :position, lastPlayedAt = :timestamp WHERE id = 1")
    suspend fun updateCurrentPosition(position: Long, timestamp: Long = System.currentTimeMillis())
    
    /**
     * 清除当前播放信息
     */
    @Query("DELETE FROM current_playlist WHERE id = 1")
    suspend fun clearCurrentPlaylist()
} 